/**  
 * All rights Reserved, Designed By <a href="www.itttq.com" target="_blank">www.itttq.com</a>
 * @date:   2018年1月20日 下午12:06:13
 * @Copyright: 2018 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */  
package com.itttq.gshop.h5.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.itttq.gshop.domain.po.SysAdPo;
import com.itttq.gshop.domain.po.SysNoticePo;
import com.itttq.gshop.domain.po.WareInfoPo;
import com.itttq.gshop.domain.to.CatBrandTo;
import com.itttq.gshop.domain.to.FloorTo;
import com.itttq.gshop.generic.connector.ISysAdConnector;
import com.itttq.gshop.generic.connector.ISysFloorConnector;
import com.itttq.gshop.generic.connector.ISysNoticeConnector;
import com.itttq.gshop.generic.connector.IWareCatBrandConnector;
import com.itttq.gshop.generic.connector.IWareInfoConnector;
import com.itttq.gshop.generic.controller.GenericController;
import com.itttq.gshop.generic.util.Constants;
import com.itttq.gshop.generic.util.Constants.AdCategory;
import com.itttq.gshop.generic.util.Constants.FloorTmpl;
import com.itttq.gshop.generic.util.Constants.WareStatus;

/**   
 * @ClassName:  IndexController
 * @author: 迷蝶
 * @date:   2018年1月20日 下午12:06:13 
 *     
 * @Copyright: 2018 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */
@RestController
public class IndexController extends GenericController {
	private static final long serialVersionUID = -6485190841311449L;
	@Autowired
	private ISysAdConnector adConnector;
	@Autowired
	private IWareInfoConnector wareConnector;
	@Autowired
	private IWareCatBrandConnector catBrandConnector;
	@Autowired
	private ISysFloorConnector floorConnector;
	@Autowired
	private ISysNoticeConnector noticeConnector;
	/**
	 * 主页
	 * @return
	 */
	@RequestMapping(value={"index.html", ""})
	public ModelAndView index() {
		ModelAndView mv = new ModelAndView("/index");
		
		WareInfoPo ware = new WareInfoPo();
		ware.setSales(WareStatus.ONSHELF.getVal());
		ware.setSize(5);
		ware.setSortField("time");
		// 获取活楼层数据前端根据楼层数据展示
		List<FloorTo> floors = floorConnector.getFloorsAndImgs();
		for(FloorTo floor : floors) {
			if(floor.getDisplayType().intValue() == FloorTmpl.HOT.getVal()) {
				// 获取热卖商品
				ware.setHot(Constants.STATUTS_OPEN);
				mv.addObject("hots", wareConnector.getWares(ware));
			} else if(floor.getDisplayType().intValue() == FloorTmpl.NEW.getVal()) {
				ware.setHot(null);
				// 获取新品商品
				mv.addObject("new", wareConnector.getWares(ware));
			}
		}
		mv.addObject("floors", floors);
		// 获取幻灯
		SysAdPo ad = new SysAdPo();
		ad.setCategory(AdCategory.SLIDE.getVal());
		ad.setStatus(Constants.STATUTS_OPEN);
		mv.addObject("slides", adConnector.getSysAds(ad).getData());
		
		// 获取白酒和红酒品牌，后续灵活TODO GXG
		Map<String, List<CatBrandTo>> catBrands = catBrandConnector.getAllEnableCatBrands();
		if(null != catBrands.get("红酒")) {
			mv.addObject("redBrands", catBrands.get("红酒").subList(0, Math.min(4, catBrands.get("红酒").size())));
		}
		if(null != catBrands.get("白酒")) {
			mv.addObject("whiteBrands", catBrands.get("白酒").subList(0, Math.min(4, catBrands.get("白酒").size())));
		}
		// 获取公告通知
		SysNoticePo notice = new SysNoticePo();
		notice.setSize(5);
		notice.setStatus(Constants.STATUTS_OPEN);
		mv.addObject("notices", noticeConnector.getNotices(notice));
		// TODO GXG 后续考虑缓存和刷新首页数据
		return mv;
	}
	
	/**
	 * 注册页面
	 * @return
	 */
	@RequestMapping("reg.html")
	public ModelAndView register(String code, HttpServletRequest request) {
		ModelAndView mv = login();
		mv.addObject("op", "reg");
		mv.addObject("promoCode", code);
		return mv;
	}

	/**
	 * 登录页面
	 * @return
	 */
	@RequestMapping("login.html")
	public ModelAndView login() {
		ModelAndView mv = new ModelAndView("/login");
		mv.addObject("op", "login");
		return mv;
	}
	
	/**
	 * 找回密码页面
	 * @return
	 */
	@RequestMapping("forgetPwd.html")
	public ModelAndView forgetPwd() {
		ModelAndView mv = login();
		mv.addObject("op", "forget");
		return mv;
	}
}
